package cinema.Controller;

import cinema.Dao.MovieDAO;
import cinema.Dao.TimeTableDAO;
import cinema.Entity.Movie;
import cinema.Entity.TimeTable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ManagedBean(name = "movieBean")
@SessionScoped
public class MovieDetailsController {

    @Inject
    MovieDAO movieDAO;
    @Inject
    private TimeTableDAO timeTableDAO;

    private List<TimeTable> showingTimes;

    private Movie movie;

    public String process() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        int mId = Integer.parseInt(params.get("movieId"));
        movie = movieDAO.findById(mId);
        filter();

        return "movieDetails?faces-redirect=true";
    }

    public void filter() {
        showingTimes = new ArrayList<TimeTable>();
        List<TimeTable> fullTimeTable = timeTableDAO.findAll();
        for (TimeTable t : fullTimeTable) {
            String title = t.getMovie().getTitle();
            if (title.equals(movie.getTitle()))
                showingTimes.add(t);
        }
    }

    public List<TimeTable> getShowingTimes() {
        return showingTimes;
    }

    public void setShowingTimes(List<TimeTable> showingTimes) {
        this.showingTimes = showingTimes;
    }

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }
}
